<?php

namespace app\admin\controller;
use think\Controller;
use app\admin\model\User;
use think\Config;
use think\Session;

class Login extends Controller
{
    public function index(){
        return $this->fetch();
    }

    public function check(){
        $uname = $this->request->param('uname');
        $upass = $this->request->param('upass');
        if($uname and $upass){
            $user = User::get(['uname'=>$uname]);
            if($user){
                if($user->upass==sha1($upass.$user['salt'])){
                    User::where('uname',$uname)->update(['last_time'=>time(),'login_counts'=>$user->login_counts+1]);
                    $roles = $user->roles()->select();
                    Session::set('kj.uname',$uname);
                    Session::set('kj.roles',$roles);
                    Session::set('kj.uid',$user->id);
                    $res = ['statusCode'=>200,'message'=>'欢迎使用'.Config::get('view_replace_str')['__APPNAME__'].'!'];
                }else{
                    $res = ['statusCode'=>300,'message'=>'对不起，您输入的密码错误'];
                }

            }else{
                $res = ['statusCode'=>300,'message'=>'对不起，您输入的账号不存在'];
            }
        } else {
            $res = ['statusCode'=>300,'message'=>'请正确输入用户名和密码'];
        }
        echo json_encode($res);
    }

    private function get_password( $length = 8 )
    {
        $str = substr(md5(time()), 0, 6);
        return $str;
    }
}